<?php

namespace app\api\command;

use app\common\model\stock\StockYtdPrice;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\model\stock\Stock;
use app\common\model\stock\StockCallAuction;
use app\common\define\StockDefine;

/**
 * 创业板
 * 获取股票集合竞价时间段的数据（工作日每天早上9:20分执行）
 * Class StockCallAuction
 * @package app\api\command
 */
class StockCallAuctionCh extends Command{

    protected function configure(){
        $this->setName('StockCallAuctionCh')->setDescription('获取股票集合竞价的实时价格（创业板）');
    }

    protected function execute(Input $input, Output $output){
        $token = StockDefine::STOCK_API_TOKEN;
        //如果是周一，则取上周五的数据
        $today = date("l");
        if ($today == 'Monday') {
            $ytdDate = date('Y-m-d', strtotime('-3 days'));
        } else {
            $ytdDate = date('Y-m-d', strtotime('-1 day'));
        }

        //获取前一天筛选出的目标列表数据
        $field = 'name,code,type,date,close_price';
        $where = [
            'type' => 300,
            'date' => $ytdDate,
        ];
        $requestUrl = 'https://stockapi.com.cn';
        while (true) { //早上数据不断更新，所以用到此处循环
            $t = time();
//            if ($t > strtotime(date('9:30'))) {
//                echo '执行完毕';
//                break;
//            }
            $ytdStockList = collection(StockYtdPrice::field($field)->where($where)->select())->toArray();

            foreach ($ytdStockList as $item) {
                $code = $item['code'];
                $requestUri = "{$requestUrl}/v1/base/second?token={$token}&code={$code}&all=1";
                $ret = get_curl_data($requestUri);
                $result = $ret[1];
                $result = json_decode($result, true);
                if (!empty($result['data'])) {
                    $list = $result['data'];
                    foreach ($list as $i) {
                        //只取9:20 到 9:26之间的数据
                        $t = strtotime(date($i['time']));
                        if ($t < strtotime(date('9:20')) || $t > strtotime(date('9:26'))) {
                            continue;
                        }

                        $timestamp = strtotime(date($i['time']));
                        //判断是否存在
                        $where1 = [
                            'code' => $code,
                            'timestamp' => $timestamp,
                        ];
                        $isExist = StockCallAuction::where($where1)->count();
                        var_dump($isExist);
                        if ($isExist) {
                            continue;
                        }

                        //写入集合竞价表，做数据分析
                        $datetime = date('Y-m-d H:i:s', strtotime(date($i['time'])));
                        $insert = [
                            'name' => $item['name'],
                            'code' => $code,
                            'type' => $item['type'],
                            'price' => $i['price'],
                            'ytd_close_price' => $item['close_price'],
                            'datetime' => $datetime,
                            'timestamp' => $timestamp,
                            'create_time' => time(),
                        ];
                        $re = StockCallAuction::insert($insert);
                        echo $item['name'].$item['code'].'处理结果为：'.$re."\n\r";
                    }

                }


            }
        }
    }

    /*protected function execute(Input $input, Output $output){
        $token = StockDefine::STOCK_API_TOKEN;

        while (true) {
            $t = time();
            if ($t > strtotime(date('9:25'))) {
                echo '执行完毕';
                break;
            }
            $field = 'name,code';
            $gupiaoList = collection(Gupiao::field($field)->select())->toArray();

            foreach ($gupiaoList as $item) {
                $requestUrl = "https://stockapi.com.cn/v1/base/second?token={$token}&code={$item['code']}&all=1";
                $ret = get_curl_data($requestUrl);
                $result = $ret[1];
                $result = json_decode($result, true);


                sleep(1);
            }
        }
    }*/
}
